﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Runtime.InteropServices;

namespace ChannelJsonConvet
{
    public class AreasData
    {
        //旧版本
        public string ids { get; set; }
        //新版本
        public string open_version { get; set; }

        public string test_version { get; set; }

        public AreasData()
        {
            test_version = "2.2.0.5";
        }

        public AreasData Clone()
        {
            AreasData data = new AreasData();
            data.ids = ids;
            data.open_version = open_version;
            data.test_version = test_version;
            return data;
        }
    }

    public class PackInfoData
    {
        //版本名
        public string version_name { get; set; }
        //APK名
        public string install_package_url { get; set; }
        //APKMd5码
        public string install_package_md5 { get; set; }
        //APK大小
        public string install_package_size { get; set; }

        public PackInfoData Clone()
        {
            PackInfoData data = new PackInfoData();
            data.version_name = version_name;
            data.install_package_url = install_package_url;
            data.install_package_md5 = install_package_md5;
            data.install_package_size = install_package_size;
            return data;
        }
    }


    public class ChannelData
    {
        //渠道名
        public string ChannelName { get; set; }
        //版本升级信息
        public List<AreasData> AreasDataList { get; set; }
        //包信息
        public List<PackInfoData> PackInfoDataList { get; set; }

        public ChannelData()
        {
            AreasDataList = new List<AreasData>();
            PackInfoDataList = new List<PackInfoData>();
        }

        public ChannelData Clone()
        {
            ChannelData data = new ChannelData();
            data.ChannelName = ChannelName;
            data.AreasDataList = AreasDataList;
            data.PackInfoDataList = PackInfoDataList;
            return data;
        }
    }

    public class OutputAreasData
    {
        public string channel_id;

        public string base_url;

        public string backup_base_url;

        public List<AreasData> areas;

        public OutputAreasData(ChannelData channelData,ConfigData configData)
        {
            channel_id = channelData.ChannelName;

            areas = channelData.AreasDataList;

            base_url = configData.m_base_url;

            backup_base_url = configData.m_backup_base_url;
        }
    }

    public class OutputPackInfoData
    {
        public string channel_id;

        public List<PackInfoData> pack_info;

        public OutputPackInfoData(ChannelData channelData)
        {
            channel_id = channelData.ChannelName;

            pack_info = channelData.PackInfoDataList;
        }
    }

    public class OutputData
    {
        public List<OutputAreasData> versions;

        public List<OutputPackInfoData> packages;

        public OutputData(List<OutputAreasData> areasDatas, List<OutputPackInfoData> packInfoDatas)
        {
            versions = areasDatas;

            packages = packInfoDatas;
        }

        public OutputData(List<ChannelData> channelDatas,ConfigData configData)
        {
            versions = new List<OutputAreasData>();
            packages = new List<OutputPackInfoData>() ;
            for (int i=0;i<channelDatas.Count;i++)
            {
                OutputAreasData outputAreasData = new OutputAreasData(channelDatas[i], configData);
                versions.Add(outputAreasData);
                OutputPackInfoData outputPackInfoData = new OutputPackInfoData(channelDatas[i]);
                packages.Add(outputPackInfoData);
            }
        }
    }


    public class ConfigData
    {
        public string m_jsonFilePath;
        public string m_apkFilePath;
        public string m_outputFilePath;
        public bool m_isSingleOutput = false;
        public string m_base_url;
        public string m_backup_base_url;
    }

    public enum Operation
    {
        Add,
        Edit,
        Delete,
    }

    public enum DataSource
    {
        ChannelsData,
        AreasData,
        PacksData,
    }

    public class OperationData
    {
        public Operation m_operation = Operation.Edit;
        public DataSource m_dataSource = DataSource.AreasData;
        public object m_originData;
        public int m_index;
        public int m_selectChannelIndex;

        public OperationData(Operation operation, DataSource dataSource,object data,int index,int selectChannelIndex)
        {
            m_operation = operation;
            m_dataSource = dataSource;
            m_originData = data;
            m_index = index;
            m_selectChannelIndex = selectChannelIndex;
        }

    }
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<ChannelData> channelsDataCollection = null;

        ObservableCollection<AreasData> areasDataCollection = null;

        ObservableCollection<PackInfoData> packsDataCollection = null;

        Dictionary<string,ChannelData> dataDic = new Dictionary<string,ChannelData>();

        List<ChannelData> channelDataList = new List<ChannelData>();

        Stack<OperationData> operationDataStack = new Stack<OperationData>();

        ConfigData configData = new ConfigData();

        //string selectChannelName = "A1";
        int selectChannelIndex = -1;


        IniFile iniFile = null;

        string iniFileName = "Config.ini";



        public MainWindow()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            this.Title = "光明勇士_渠道Json编辑工具";
            Init();
        }

        void Init()
        {
            channelsDataCollection = null;
            areasDataCollection = null;
            packsDataCollection = null;
            dataDic.Clear();
            operationDataStack.Clear();
            channelDataList.Clear();
            selectChannelIndex = -1;

            InitConfig();
            LoadJson();
            InitLBChannelName();
        }

        void InitLBChannelName()
        {
            foreach (ChannelData channelData in dataDic.Values)
            {
                channelDataList.Add(channelData);
            }
            channelsDataCollection = new ObservableCollection<ChannelData>(channelDataList);
            this.lbChannelName.ItemsSource = channelsDataCollection;
            if (lbChannelName.Items.Count == 0)
            {
                return;
            }
            lbChannelName.SelectedItem = lbChannelName.Items[0];
            //selectChannelName = (lbChannelName.SelectedItem as ChannelData).ChannelName;
            selectChannelIndex = lbChannelName.SelectedIndex;
            RefreshAreasList(selectChannelIndex);
            RefreshPacksList(selectChannelIndex);
        }

        void RefreshAreasList(int index)
        {
            if (index >= channelDataList.Count) return;
            areasDataCollection = new ObservableCollection<AreasData>(channelDataList[index].AreasDataList);
            this.lbAreasData.ItemsSource = areasDataCollection;
        }

        void RefreshPacksList(int index)
        {
            if (index >= channelDataList.Count) return;
            packsDataCollection = new ObservableCollection<PackInfoData>(channelDataList[index].PackInfoDataList);
            this.lbPackInfoData.ItemsSource = packsDataCollection;
        }

        void InitConfig()
        {
            iniFile = new IniFile(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\" + iniFileName);
            configData.m_jsonFilePath = iniFile.IniReadValue("通用设置", "Json路径");
            configData.m_apkFilePath = iniFile.IniReadValue("通用设置", "Apk路径");
            configData.m_outputFilePath = iniFile.IniReadValue("导出设置", "文件导出路径");
            configData.m_isSingleOutput = iniFile.IniReadValue("导出设置", "是否单个文件导出") == "1" ? true : false;
            configData.m_base_url = iniFile.IniReadValue("通用设置", "base_url");
            configData.m_backup_base_url = iniFile.IniReadValue("通用设置", "m_backup_base_url");
            if (configData.m_base_url == "") configData.m_base_url = "http://gmys.webpatch.sdg-china.com/apkupdate";
            if (configData.m_backup_base_url == "") configData.m_backup_base_url = "https://gmys.webpatch.sdg-china.com/apkupdate";
            if (configData.m_jsonFilePath == "" || !Directory.Exists(configData.m_jsonFilePath))
            {
                configData.m_jsonFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Json";
                if (!Directory.Exists(configData.m_jsonFilePath))
                {
                    Directory.CreateDirectory(configData.m_jsonFilePath);
                }
            }

            if(configData.m_apkFilePath == "" || !Directory.Exists(configData.m_apkFilePath))
            {
                configData.m_apkFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Apk";
                if (!Directory.Exists(configData.m_apkFilePath))
                {
                    Directory.CreateDirectory(configData.m_apkFilePath);
                }
            }

            if(configData.m_outputFilePath == "" || !Directory.Exists(configData.m_outputFilePath))
            {
                configData.m_outputFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Json";
                if (!Directory.Exists(configData.m_outputFilePath))
                {
                    Directory.CreateDirectory(configData.m_outputFilePath);
                }
            }
        }

        void LoadJson()
        {
            List<string> filePathList = new List<string>();
            GetJsonFile(configData.m_jsonFilePath, filePathList);
            for(int i=0;i< filePathList.Count;i++)
            {
                LoadJsonInfo(filePathList[i]);
            }
        }

        void GetJsonFile(string jsonFilePath,List<string> filePathList)
        {
            DirectoryInfo dir = new DirectoryInfo(jsonFilePath);
            if (dir == null)
            {
                return;
            }

            try
            {
                FileSystemInfo[] fileSysInfos = dir.GetFileSystemInfos();
                foreach (FileSystemInfo fileSysInfo in fileSysInfos)
                {
                    FileInfo fileInfo = fileSysInfo as FileInfo;
                    if(fileInfo != null)
                    {
                        if (fileInfo.Name.Contains(".json"))
                        {
                            filePathList.Add(fileInfo.FullName);
                        }
                    }
                    else
                    {
                        string childDirName = fileSysInfo.Name;
                        GetJsonFile(jsonFilePath + @"\" + childDirName, filePathList);
                    }

                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e.ToString());
                ShowTipWnd(this, "错误", e.ToString());
            }
        }

        //解析Json
        void LoadJsonInfo(string path)
        {
            using (StreamReader file = File.OpenText(path))
            {
                using(JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject jsonObect = (JObject)JToken.ReadFrom(reader);
                    var versions = jsonObect["versions"];
                    foreach(JObject child in versions)
                    {
                        string key = child["channel_id"].ToString();
                        var areas = child["areas"];
                        List<AreasData> areasDataList = new List<AreasData>();
                        foreach (JObject grandChild in areas)
                        {
                            AreasData areasData = new AreasData();
                            areasData.ids = grandChild["ids"].ToString();
                            areasData.open_version = grandChild["open_version"].ToString();
                            areasDataList.Add(areasData);
                        }

                        ChannelData item = null;
                        if(dataDic.TryGetValue(key, out item))
                        {
                            item.AreasDataList = areasDataList;
                        }
                        else
                        {
                            item = new ChannelData();
                            item.ChannelName = key;
                            item.AreasDataList = areasDataList;
                            dataDic.Add(key, item);
                        }
                    }

                    var packages = jsonObect["packages"];
                    foreach (JObject child in packages)
                    {
                        string key = child["channel_id"].ToString();
                        var pack = child["pack_info"];
                        List<PackInfoData> packInfoDataList = new List<PackInfoData>();
                        foreach (JObject grandChild in pack)
                        {
                            PackInfoData packInfoData = new PackInfoData();
                            packInfoData.version_name = grandChild["version_name"].ToString();
                            packInfoData.install_package_url = grandChild["install_package_url"].ToString();
                            packInfoData.install_package_md5 = grandChild["install_package_md5"].ToString();
                            packInfoData.install_package_size = grandChild["install_package_size"].ToString();
                            packInfoDataList.Add(packInfoData);
                        }

                        ChannelData item = new ChannelData();
                        if (dataDic.TryGetValue(key, out item))
                        {
                            item.PackInfoDataList = packInfoDataList;
                        }
                        else
                        {
                            item.ChannelName = key;
                            item.PackInfoDataList = packInfoDataList;
                            dataDic.Add(key, item);
                        }
                    }
                }
            }
        }

        private void lbChannelName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(isChannelDelete)
            {
                return;
            }
            if(lbChannelName.SelectedItem == null)
            {
                return;
            }
            RefreshChannel(lbChannelName.SelectedIndex);
        }

        void RefreshChannel(int index )
        {
            //selectChannelName = name;

            selectChannelIndex = index;
            lbChannelName.SelectedIndex = selectChannelIndex;
            RefreshAreasList(index);
            RefreshPacksList(index);
        }

        private void lbAreasData_MouseRightButtonUp(object sender, MouseEventArgs e)
        {
            lbAreasData_ContextMenu.Visibility = System.Windows.Visibility.Visible;
        }


        //右键菜单的点击事件
        //删除事件
        private void lbAreasData_Delete_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbAreasData.SelectedIndex;
            if(selectedIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中的数据，无法删除");
                return;
            }
            OperationData operationData = new OperationData(Operation.Delete, 
                DataSource.AreasData, channelDataList[selectChannelIndex].AreasDataList[selectedIndex], selectedIndex,selectChannelIndex);
            operationDataStack.Push(operationData);
            areasDataCollection.Remove(channelDataList[selectChannelIndex].AreasDataList[selectedIndex]);
            channelDataList[selectChannelIndex].AreasDataList.RemoveAt(selectedIndex);
        }

        bool isChannelDelete = false;
        private void lbChannelName_Delete_Click(object sender, RoutedEventArgs e)
        {
            if (selectChannelIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中的数据，无法删除");
                return;
            }
            isChannelDelete = true;
            int selectedIndex = lbChannelName.SelectedIndex;
            OperationData operationData = new OperationData(Operation.Delete, 
                DataSource.ChannelsData, channelDataList[selectChannelIndex], selectedIndex, selectChannelIndex);
            operationDataStack.Push(operationData);

            channelsDataCollection.Remove(channelDataList[selectChannelIndex]);
            channelDataList.RemoveAt(selectChannelIndex);
            isChannelDelete = false; 
            if (lbChannelName.Items.Count != 0)
            {
                lbChannelName.SelectedItem = lbChannelName.Items[0];
                selectChannelIndex = lbChannelName.SelectedIndex;
            }
            else
            {
                if (areasDataCollection != null)
                {
                    areasDataCollection.Clear();
                }
                if (packsDataCollection != null)
                {
                    packsDataCollection.Clear();
                }
                selectChannelIndex = -1;
            }

        }

        private void lbPackInfoData_Delete_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbPackInfoData.SelectedIndex;
            if (selectedIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中的数据，无法删除");
                return;
            }
            OperationData operationData = new OperationData(Operation.Delete,
               DataSource.PacksData, channelDataList[selectChannelIndex].PackInfoDataList[selectedIndex], selectedIndex, selectChannelIndex);
            operationDataStack.Push(operationData);
            packsDataCollection.Remove(channelDataList[selectChannelIndex].PackInfoDataList[selectedIndex]);
            channelDataList[selectChannelIndex].PackInfoDataList.RemoveAt(selectedIndex);
        }

     

        private void lbPackInfoData_Editor_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbPackInfoData.SelectedIndex;
            if (selectedIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中的数据，无法编辑");
                return;
            }
            PackInfoData packInfoData = channelDataList[selectChannelIndex].PackInfoDataList[selectedIndex].Clone();
            PackInfoWnd packInfoWnd = new PackInfoWnd(packInfoData, channelDataList[selectChannelIndex].ChannelName, configData.m_apkFilePath);
            packInfoWnd.Title = "package信息编辑";
            packInfoWnd.Owner = this;
            if(packInfoWnd.ShowDialog().Value)
            {
                OperationData operationData = new OperationData(Operation.Edit,
                    DataSource.PacksData, channelDataList[selectChannelIndex].PackInfoDataList[selectedIndex], selectedIndex, selectChannelIndex);
                operationDataStack.Push(operationData);

                packsDataCollection[selectedIndex] = packInfoData;
                channelDataList[selectChannelIndex].PackInfoDataList[selectedIndex] = packInfoData;
            }
            lbPackInfoData.SelectedIndex = selectedIndex;

        }

        private void lbPackInfoData_new_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbPackInfoData.SelectedIndex;
            if (selectChannelIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中渠道，无法新建Package信息");
                return;
            }
            PackInfoWnd packInfoWnd = new PackInfoWnd(channelDataList[selectChannelIndex].ChannelName, configData.m_apkFilePath);
            packInfoWnd.Title = "package信息新建";
            packInfoWnd.Owner = this;
            if (packInfoWnd.ShowDialog().Value)
            {
                OperationData operationData = new OperationData(Operation.Add,
                   DataSource.PacksData, null, selectedIndex + 1, selectChannelIndex);
                operationDataStack.Push(operationData);

                PackInfoData packInfoData = packInfoWnd.m_data;
                packsDataCollection.Insert(selectedIndex+1,packInfoData);
                channelDataList[selectChannelIndex].PackInfoDataList.Insert(selectedIndex + 1, packInfoData);

            }

            lbPackInfoData.SelectedIndex = selectedIndex + 1;
        }

        private void lbAreasData_Editor_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbAreasData.SelectedIndex;
            if(selectedIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中的数据，无法编辑");
                return;
            }
            AreasData areasData = channelDataList[selectChannelIndex].AreasDataList[selectedIndex].Clone();
            AreasWnd areasWnd = new AreasWnd(areasData, channelDataList[selectChannelIndex].ChannelName);
            areasWnd.Title = "areas信息编辑";
            areasWnd.Owner = this;
            if (areasWnd.ShowDialog().Value)
            {
                OperationData operationData = new OperationData(Operation.Edit,
                     DataSource.AreasData, channelDataList[selectChannelIndex].AreasDataList[selectedIndex], selectedIndex, selectChannelIndex);
                operationDataStack.Push(operationData);

                areasDataCollection[selectedIndex] = areasData;
                channelDataList[selectChannelIndex].AreasDataList[selectedIndex] = areasData;
            }

            lbAreasData.SelectedIndex = selectedIndex;
        }

        private void lbAreasData_new_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbAreasData.SelectedIndex;
            if(selectChannelIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中渠道，无法新建版本信息");
                return;
            }
            AreasWnd areasWnd = new AreasWnd(channelDataList[selectChannelIndex].ChannelName);
            areasWnd.Title = "areas信息新建";
            areasWnd.Owner = this;
            if (areasWnd.ShowDialog().Value)
            {
                OperationData operationData = new OperationData(Operation.Add,
                    DataSource.AreasData, null, selectedIndex + 1, selectChannelIndex);
                operationDataStack.Push(operationData);

                AreasData areasData = areasWnd.m_data;
                areasDataCollection.Insert(selectedIndex+1, areasData);
                channelDataList[selectChannelIndex].AreasDataList.Insert(selectedIndex+1,areasData);
            }

            lbAreasData.SelectedIndex = selectedIndex + 1;
        }

        private void lbChannelName_Editor_Click(object sender, RoutedEventArgs e)
        {
            if (selectChannelIndex == -1)
            {
                ShowTipWnd(this, "提示", "没有选中的数据，无法编辑");
                return;
            }
            ChannelNameWnd channelNameWnd = new ChannelNameWnd(channelDataList[selectChannelIndex].ChannelName);
            channelNameWnd.Owner = this;
            channelNameWnd.Title = "渠道名编辑";
            if (channelNameWnd.ShowDialog().Value)
            {
                OperationData operationData = new OperationData(Operation.Edit,
                    DataSource.ChannelsData, channelDataList[selectChannelIndex], selectChannelIndex, selectChannelIndex);
                operationDataStack.Push(operationData);

                ChannelData channelData = channelDataList[selectChannelIndex].Clone();
                channelData.ChannelName = channelNameWnd.m_channelName;
                channelsDataCollection[selectChannelIndex] = channelData;
                channelDataList[selectChannelIndex] = channelData;
            }
        }

        private void lbChannelName_new_Click(object sender, RoutedEventArgs e)
        {
            ChannelNameWnd channelNameWnd = new ChannelNameWnd();
            channelNameWnd.Owner = this;
            channelNameWnd.Title = "渠道新建";
            if (channelNameWnd.ShowDialog().Value)
            {
                OperationData operationData = new OperationData(Operation.Add,
                    DataSource.ChannelsData, null, selectChannelIndex + 1, selectChannelIndex);
                operationDataStack.Push(operationData);

                ChannelData channelData = new ChannelData();
                channelData.ChannelName = channelNameWnd.m_channelName;

                channelsDataCollection.Insert(selectChannelIndex + 1, channelData);
                channelDataList.Insert(selectChannelIndex + 1, channelData);
            }

            RefreshChannel(selectChannelIndex + 1);

        }

        public static void ShowTipWnd(Window parent, string title,string tip)
        {
            TipWnd wnd = new TipWnd(title,tip);
            wnd.Title = title;
            if(parent.IsActive)
            wnd.Owner = parent;
            wnd.ShowDialog();

        }


        //设置按键点击事件
        private void ConfigButton_Click(object sender, RoutedEventArgs e)
        {
            ConfigSetting configWnd = new ConfigSetting(configData);
            configWnd.Owner = this;
            if (configWnd.ShowDialog().Value)
            {
                configData.m_apkFilePath = configWnd.apkFilePath;
                configData.m_jsonFilePath = configWnd.jsonFilePath;
                configData.m_outputFilePath = configWnd.ouputFilePath;
                configData.m_isSingleOutput = configWnd.isSingleOutput;
                configData.m_base_url = configWnd.base_url;
                configData.m_backup_base_url = configWnd.backup_base_url;
                SaveConfig();
            }
        }

        void SaveConfig()
        {
            iniFile.IniWriteValue("通用设置", "Json路径", configData.m_jsonFilePath);
            iniFile.IniWriteValue("通用设置", "Apk路径", configData.m_apkFilePath);
            iniFile.IniWriteValue("导出设置", "文件导出路径", configData.m_outputFilePath);
            iniFile.IniWriteValue("导出设置", "是否单个文件导出", configData.m_isSingleOutput ? "1" : "0");
            iniFile.IniWriteValue("通用设置", "base_url", configData.m_base_url);
            iniFile.IniWriteValue("通用设置", "backup_base_url", configData.m_backup_base_url);
        }

        //重新载入按键点击事件
        private void ReStartButton_Click(object sender, RoutedEventArgs e)
        {
            if (areasDataCollection != null)
            {
                areasDataCollection.Clear();
            }
            if (packsDataCollection != null)
            {
                packsDataCollection.Clear();
            }
            Init();
        }

        //撤销按键点击事件
        private void ReSetButton_Click(object sender, RoutedEventArgs e)
        {
            if (operationDataStack.Count == 0)
            {
                ShowTipWnd(this, "提示", "已经没有可撤回的操作了");
                return;
            }

            OperationData operationData = operationDataStack.Pop();
            DoOperation(operationData);

        }

        private void OuputButton_Click(object sender, RoutedEventArgs e)
        {
            if(configData.m_isSingleOutput)
            {
                OutputData outputData = new OutputData(channelDataList, configData);
                WriteJson(outputData, configData.m_outputFilePath + @"\ver_ctrl.json");
            }
            else
            {
                for(int i=0;i<channelDataList.Count;i++)
                {
                    List<ChannelData> channelDatas = new List<ChannelData>() { channelDataList[i]};
                    OutputData outputData = new OutputData(channelDatas, configData);
                    WriteJson(outputData, configData.m_outputFilePath + @"\"+ channelDataList[i].ChannelName+"_ver_ctrl.json");
                }
            }
        }

        void WriteJson(OutputData outputData,string wirtePath)
        {
            string outputJsonStr = JsonConvert.SerializeObject(outputData);
            outputJsonStr = ConvertJsonString(outputJsonStr);
            try
            {
                File.WriteAllText(wirtePath, outputJsonStr);
            }
            catch(Exception e)
            {
                ShowTipWnd(this, "错误", e.ToString());
            }
        }

        private string ConvertJsonString(string str)
        {
            //格式化json字符串
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }




        void DoOperation(OperationData originData)
        {
            if (originData.m_dataSource == DataSource.AreasData)
            {
                RefreshChannel(originData.m_selectChannelIndex);
                AreasData areasData = originData.m_originData as AreasData;
                if (originData.m_operation == Operation.Delete)
                {
                    areasDataCollection.Insert(originData.m_index, areasData);
                    channelDataList[originData.m_selectChannelIndex].AreasDataList.Insert(originData.m_index, areasData);
                    lbAreasData.SelectedIndex = originData.m_index;
                }
                else if (originData.m_operation == Operation.Add)
                {
                    areasDataCollection.RemoveAt(originData.m_index);
                    channelDataList[originData.m_selectChannelIndex].AreasDataList.RemoveAt(originData.m_index);
                    lbAreasData.SelectedIndex = originData.m_index - 1;
                }
                else if (originData.m_operation == Operation.Edit)
                {
                    areasDataCollection[originData.m_index] = areasData;
                    channelDataList[originData.m_selectChannelIndex].AreasDataList[originData.m_index] = areasData;
                    lbAreasData.SelectedIndex = originData.m_index;
                }
            }
            else if (originData.m_dataSource == DataSource.ChannelsData)
            {
                ChannelData channelData = originData.m_originData as ChannelData;
                if (originData.m_operation == Operation.Delete)
                {
                    channelsDataCollection.Insert(originData.m_index, channelData);
                    channelDataList.Insert(originData.m_index, channelData);
                    lbChannelName.SelectedIndex = originData.m_index;
                }
                else if (originData.m_operation == Operation.Add)
                {
                    channelsDataCollection.RemoveAt(originData.m_index);
                    channelDataList.RemoveAt(originData.m_index);
                    lbChannelName.SelectedIndex = originData.m_index - 1;
                }
                else if (originData.m_operation == Operation.Edit)
                {
                    channelsDataCollection[originData.m_index] = channelData;
                    channelDataList[originData.m_index] = channelData;
                    lbChannelName.SelectedIndex = originData.m_index;
                }
            }
            else if (originData.m_dataSource == DataSource.PacksData)
            {
                RefreshChannel(originData.m_selectChannelIndex);
                PackInfoData packInfoData = originData.m_originData as PackInfoData;
                if (originData.m_operation == Operation.Delete)
                {
                    packsDataCollection.Insert(originData.m_index, packInfoData);
                    channelDataList[originData.m_selectChannelIndex].PackInfoDataList.Insert(originData.m_index, packInfoData);
                    lbPackInfoData.SelectedIndex = originData.m_index;
                }
                else if (originData.m_operation == Operation.Add)
                {
                    packsDataCollection.RemoveAt(originData.m_index);
                    channelDataList[originData.m_selectChannelIndex].PackInfoDataList.RemoveAt(originData.m_index);
                    lbPackInfoData.SelectedIndex = originData.m_index - 1;
                }
                else if (originData.m_operation == Operation.Edit)
                {
                    packsDataCollection[originData.m_index] = packInfoData;
                    channelDataList[originData.m_selectChannelIndex].PackInfoDataList[originData.m_index] = packInfoData;
                    lbPackInfoData.SelectedIndex = originData.m_index;
                }
            }
        }


    }

}
